<?php
/**
 *     
 *  游戏生成订单 
 */

class PlaceOrder extends APIBase {
	public $tag = "PlaceOrder";
	public $isLogin = true;
	
	public function before() {
		$this->initMysql();
		return true;
	}
		
	//生成随机24位订单号
	protected function createOrderId ($length = 32) {
		$seed=md5(microtime());
		$pwd='';
		for($i=0;$i<$length;$i++){
			$pwd.=$seed{mt_rand(0,31)};
		}
		$hash=md5($pwd);
		return substr($hash,0,$length);
	}
	
    //进行公钥加密
    protected function public_encrypt($input) {
        $public_key = file_get_contents(__dir__ .'/IMpay/public_key.txt');		
        openssl_public_encrypt($input,$output,$public_key);  
        return base64_encode($output);  
    }
	
   
	public function logic() {
        //商品id
		if (!isset($this->param['goodsid'])) {
			$this->returnError(310, "goodsid must be needed");
		}
		
		//支付方式 （pay_type 0,微信支付 1,支付宝支付）
		if (!isset($this->param['pay_type'])) {
			$this->returnError(310, "pay_type must be needed");
		}
		
		
		//判断平台 1安卓  2苹果
		//if (!isset($this->param['platform']) || $this->param['platform'] == '') {
			//$this->returnError(310, "platform must be needed");
		//}
		
		//判断是否独立包
		//if (isset($this->param['independent']) && ($this->param['independent'] != 0 || $this->param['independent'] != '')){
			//$independent = 1;
		//}else{
			//$independent = 0;
		//}
		
		//app_id (支付的app_id，长沙麻将独立包为1，平台长沙麻将是14
		/*
		if ($independent == 1){
			$app_id = 1;
			$notify_url = Config::$independent_notify_url;
		}else{
			$app_id = 14;
			$notify_url = Config::$platform_notify_url;
		}
		*/
		
		//获取玩家信息
		$userinfo = $this->data_redis->hGetAll("hu:".(int) $this->uid);
		$this->d(json_encode($userinfo));
		if (!empty($userinfo)){
			$accid = $userinfo['accid'];
		}else{
			$this->returnError(310, "accid must be needed");
		}
		
		
		
        //根据id查询出商品信息
		$where['id'] = (int)$this->param['goodsid'];
		$good = $this->mysql->select('goods','*',$where);

		if(!count($good)) {     
			$this->returnError(310, "goodsid is not exists");
		}
		
		$attach = json_encode(array('goodsid'=> (int) $this->param['goodsid']));
		
		//订单id
		$orderid = $this->createOrderId();
		$payinfo['uid'] = (int) $this->uid;
		$payinfo['app_id'] = 0;
		$payinfo['price'] = (int) $good[0]['buy_money'] * 100;
		$payinfo['pay_type'] = (int) $this->param['pay_type'];
		$payinfo['goodsid'] = (int) $this->param['goodsid'];
		$payinfo['version'] = 0;
		$payinfo['create_time'] = time();
		$payinfo['type'] = 0;
		$payinfo['is_send'] = 0;
		$payinfo['orderid'] = 'csmj00'.substr($orderid,6,26);

		$sign  = $this->mysql->insert('applypay', $payinfo);
		//$payinfo['orderid'] = self::public_encrypt($payinfo['orderid']);
		/*
		$data = array('pay'=>array('accid' => (int) $accid,
												 'app_id' => (int) $app_id,
												 'subject' => $good[0]['name'],
												 'amount' => (int) $good[0]['buy_money'] * 100,
												 'pay_type' => (int) $this->param['pay_type'],
												 'process_type' => 0,//0:直接购买, 1:充值
												 'order_id' => $payinfo['orderid'],
												 'attach' => $attach,
												 'notify_url' => $notify_url,
												 'source' => Config::$source,//宝石10001，长沙麻将10004，卡五星10014
											     'goods_type' => (int) $good[0]['propid']));
		*/
		
		//echo strlen($ordierid);
		$data = array('pay'=>array('orderid'=>$payinfo['orderid'],'id'=>$payinfo['goodsid'],'pay_type'=>$payinfo['pay_type'],'price'=>$payinfo['price']));
		if ($sign) {
			$this->d(json_encode($data));
			$this->returnData($data);
		} else {
			$this->returnError(301,'create order failed!');
		}
	}
    
	public function after() {
		$this->deinitMysql();
	}
}

?>
